package com.ruoyi.magazine.order.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.magazine.magazine.domain.Magazine;
import com.ruoyi.magazine.magazine.service.IMagazineService;
import com.ruoyi.magazine.subscribe.domain.Subscribe;
import com.ruoyi.magazine.subscribe.service.ISubscribeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.magazine.order.mapper.OrderMapper;
import com.ruoyi.magazine.order.domain.Order;
import com.ruoyi.magazine.order.service.IOrderService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 订单Service业务层处理
 *
 * @author duanxinli
 * @date 2023-11-13
 */
@Service
public class OrderServiceImpl implements IOrderService {
    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private ISubscribeService subscribeService;
    @Autowired
    private IMagazineService magazineService;

    /**
     * 查询订单
     *
     * @param id 订单主键
     * @return 订单
     */
    @Override
    public Order selectOrderById(String id) {
        Order orderItem = orderMapper.selectOrderById(id);
        Subscribe subscribe = subscribeService.selectSubscribeByOrderId(orderItem.getId());
        Magazine magazine = magazineService.selectMagazineById(subscribe.getMagazineId());
        orderItem.setMagazineName(magazine.getMagazineName());
        orderItem.setNum(subscribe.getNum());
        orderItem.setPrice(magazine.getPrice());
        orderItem.setMagazineId(subscribe.getMagazineId());


        return orderItem;
    }

    /**
     * 查询订单列表
     *
     * @param order 订单
     * @return 订单
     */
    @Override
    public List<Order> selectOrderList(Order order) {
        List<Order> queryOrderList = orderMapper.selectOrderList(order);
        for (Order orderItem : queryOrderList) {
            Subscribe subscribe = subscribeService.selectSubscribeByOrderId(orderItem.getId());
            Magazine magazine = magazineService.selectMagazineById(subscribe.getMagazineId());
            orderItem.setMagazineName(magazine.getMagazineName());
            orderItem.setNum(subscribe.getNum());
        }
        return queryOrderList;
    }

    @Override
    public List<Order> selectUserOrderList(Order order) {
        List<Order> queryOrderList = orderMapper.selectUserOrderList(order);
        for (Order orderItem : queryOrderList) {
            Subscribe subscribe = subscribeService.selectSubscribeByOrderId(orderItem.getId());
            Magazine magazine = magazineService.selectMagazineById(subscribe.getMagazineId());
            orderItem.setMagazineName(magazine.getMagazineName());
            orderItem.setNum(subscribe.getNum());
        }
        return queryOrderList;
    }

    /**
     * 新增订单
     *
     * @param order 订单
     * @return 结果
     */
    @Override
    @Transactional
    public int insertOrder(Order order) {
        String orderId = IdUtils.fastUUID();
        Subscribe subscribe = new Subscribe();
        subscribe.setId(IdUtils.fastUUID());
        subscribe.setOrderId(orderId);
        subscribe.setNum(order.getNum());
        subscribe.setMagazineId(order.getMagazineId());
        //订单
        subscribeService.insertSubscribe(subscribe);
        order.setId(orderId);
        //待付款
        order.setStatue("0");
        order.setCreateDate(DateUtils.getNowDate());
        return orderMapper.insertOrder(order);
    }

    /**
     * 修改订单
     *
     * @param order 订单
     * @return 结果
     */
    @Override
    @Transactional
    public int updateOrder(Order order) {
        Subscribe subscribe = subscribeService.selectSubscribeByOrderId(order.getId());
        subscribe.setNum(order.getNum());
        subscribeService.updateSubscribe(subscribe);
        //从财务出获得订单号，说明付款成功
        if (order.getOrderNo() != null && order.getOrderNo() != "") {
            //已付款
            order.setStatue("2");
        }
        return orderMapper.updateOrder(order);
    }

    /**
     * 批量删除订单
     *
     * @param ids 需要删除的订单主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteOrderByIds(String[] ids) {
        for (String id : ids) {
            Subscribe subscribe = subscribeService.selectSubscribeByOrderId(id);
            subscribeService.deleteSubscribeById(subscribe.getId());
        }
        return orderMapper.deleteOrderByIds(ids);
    }

    /**
     * 删除订单信息
     *
     * @param id 订单主键
     * @return 结果
     */
    @Override
    public int deleteOrderById(String id) {
        return orderMapper.deleteOrderById(id);
    }
}
